#!/usr/bin/python
# -*-coding:utf-8-*-
# pip install ddddocr 图片识别
"""农产品质量追溯管理平台"""
import math
import time
import re
import requests
import json
from bs4 import BeautifulSoup
from time import sleep
import common
import db
from datetime import datetime
from dotenv import load_dotenv, find_dotenv
import os
import asyncio
from pyppeteer import launch
import numpy as np
import pandas as pd
import os

mysqldb = db.DbManager()

load_dotenv(verbose=True)
# 今天
today_time = common.TodayTime()
# 将-替换为.
today_time_new = today_time.replace('-', '.')
# 账号
username = os.getenv('LOGIN_ADMIN')
passworld = os.getenv('LOGIN_PWD')

# 获取登录后cookie
async def get_cookie(page):
    cookies_list = await page.cookies()
    cookies = ''
    for cookie in cookies_list:
        str_cookie = '{0}={1};'
        str_cookie = str_cookie.format(cookie.get('name'), cookie.get('value'))
        cookies += str_cookie
    print(cookies)
    return cookies


async def login():
    # 查询文件是否存在 url
    with open("link.txt", 'r+') as file:
        a = file.read().splitlines()
        file.seek(0)  # 定位
        # 清空文件内容
        file.truncate()
    print(len(a))
    if len(a)==0:
        exit()
    browser = await launch(headless=True, args=['--disable-infobars', '--no-sandbox'])
    page = await browser.newPage()
    await page.setUserAgent(
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/537.75.14")
    await page.setViewport(viewport={'width': 1536, 'height': 768})
    await page.evaluateOnNewDocument('() =>{ Object.defineProperties(navigator,'
                                     '{ webdriver:{ get: () => false } }) }')
    for link in range(0,len(a)):
        print(a[link])
        log_url = "http://fgw.lyg.gov.cn"+a[link]
        await page.goto(log_url)
        li_arr = await page.querySelectorAllEval(
            'body > div.warp1 > div.list > div > div.pp2',
            'nodes=>nodes.map(node=>node.innerHTML)')
        print(li_arr)
        # 拆分获取日期  更新时间：
        time_str = re.split("更新时间：", li_arr[0])
        time_arr=re.split(" ",time_str[1])
        # 拼接日期
        print(time_arr[0]+" 00:00")
        # 获取标题
        # body > div.warp1 > div.list > div > div.pp1 > h1
        title_arr = await page.querySelectorAllEval(
            'body > div.warp1 > div.list > div > div.pp1 > h1',
            'nodes=>nodes.map(node=>node.innerHTML)')
        print(title_arr[0])
        # 获取正式数据
        # body > div.warp1 > div.list > div > div.pp3.mt20 > table > tbody > tr
        tr_arr = await page.querySelectorAllEval(
            'body > div.warp1 > div.list > div > div.pp3.mt20 > table > tbody > tr',
            'nodes=>nodes.map(node=>node.innerHTML)')
        print(tr_arr)
        print(len(tr_arr))
        for i in range(0,len(tr_arr)):
            if i <=2:
                continue
            if i==len(tr_arr)-1:
                continue
            print(tr_arr[i])
            # body > div.warp1 > div.list > div > div.pp3.mt20 > table > tbody > tr:nth-child(4) > td:nth-child(1)
            td_len=await page.querySelectorAllEval(
                'body > div.warp1 > div.list > div > div.pp3.mt20 > table > tbody > tr:nth-child(' + str(
                    i + 1) + ') > td',
                'nodes=>nodes.map(node=>node.innerHTML)')
            name_arr = await page.querySelectorAllEval(
                'body > div.warp1 > div.list > div > div.pp3.mt20 > table > tbody > tr:nth-child(' + str(
                    i + 1) + ') > td:nth-child(1)',
                'nodes=>nodes.map(node=>node.innerHTML)')
            # body > div.warp1 > div.list > div > div.pp3.mt20 > table > tbody > tr:nth-child(4) > td:nth-child(2)
            type_arr = await page.querySelectorAllEval(
                'body > div.warp1 > div.list > div > div.pp3.mt20 > table > tbody > tr:nth-child(' + str(
                    i + 1) + ') > td:nth-child(2)',
                'nodes=>nodes.map(node=>node.innerHTML)')
            # body > div.warp1 > div.list > div > div.pp3.mt20 > table > tbody > tr:nth-child(4) > td:nth-child(4)
            price_dhx = await page.querySelectorAllEval(
                'body > div.warp1 > div.list > div > div.pp3.mt20 > table > tbody > tr:nth-child(' + str(
                    i + 1) + ') > td:nth-child(4)',
                'nodes=>nodes.map(node=>node.innerHTML)')
            # body > div.warp1 > div.list > div > div.pp3.mt20 > table > tbody > tr:nth-child(4) > td:nth-child(5)
            # body > div.warp1 > div.list > div > div.pp3.mt20 > table > tbody > tr:nth-child(4) > td:nth-child(5)
            price_gyx = await page.querySelectorAllEval(
                'body > div.warp1 > div.list > div > div.pp3.mt20 > table > tbody > tr:nth-child(' + str(
                    i + 1) + ') > td:nth-child(5)',
                'nodes=>nodes.map(node=>node.innerHTML)')
            price_gnx = await page.querySelectorAllEval(
                'body > div.warp1 > div.list > div > div.pp3.mt20 > table > tbody > tr:nth-child(' + str(
                    i + 1) + ') > td:nth-child(6)',
                'nodes=>nodes.map(node=>node.innerHTML)')
            price_avg_1 = await page.querySelectorAllEval(
                'body > div.warp1 > div.list > div > div.pp3.mt20 > table > tbody > tr:nth-child(' + str(
                    i + 1) + ') > td:nth-child(3)',
                'nodes=>nodes.map(node=>node.innerHTML)')
            price_avg_2 = await page.querySelectorAllEval(
                'body > div.warp1 > div.list > div > div.pp3.mt20 > table > tbody > tr:nth-child(' + str(
                    i + 1) + ') > td:nth-child(7)',
                'nodes=>nodes.map(node=>node.innerHTML)')
            # 如果不存在海州
            if len(td_len)==10:
                price_hzq = await page.querySelectorAllEval(
                    'body > div.warp1 > div.list > div > div.pp3.mt20 > table > tbody > tr:nth-child(' + str(
                        i + 1) + ') > td:nth-child(8)',
                    'nodes=>nodes.map(node=>node.innerHTML)')
                price_lyq = await page.querySelectorAllEval(
                    'body > div.warp1 > div.list > div > div.pp3.mt20 > table > tbody > tr:nth-child(' + str(
                        i + 1) + ') > td:nth-child(9)',
                    'nodes=>nodes.map(node=>node.innerHTML)')
                price_gyq = await page.querySelectorAllEval(
                    'body > div.warp1 > div.list > div > div.pp3.mt20 > table > tbody > tr:nth-child(' + str(
                        i + 1) + ') > td:nth-child(10)',
                    'nodes=>nodes.map(node=>node.innerHTML)')
            else:
                price_hzq=["'0'"]
                price_lyq = await page.querySelectorAllEval(
                    'body > div.warp1 > div.list > div > div.pp3.mt20 > table > tbody > tr:nth-child(' + str(
                        i + 1) + ') > td:nth-child(8)',
                    'nodes=>nodes.map(node=>node.innerHTML)')
                price_gyq = await page.querySelectorAllEval(
                    'body > div.warp1 > div.list > div > div.pp3.mt20 > table > tbody > tr:nth-child(' + str(
                        i + 1) + ') > td:nth-child(9)',
                    'nodes=>nodes.map(node=>node.innerHTML)')
            # print(price_dhx[0])
            # print(price_gyx[0])
            # print(price_gnx[0])
            # print(price_hzq[0])
            # print(price_lyq[0])
            # print(price_gyq[0])
            # continue
            price_dhx=price_dhx[0].strip('\u3000\u3000')
            price_gyx=price_gyx[0].strip('\u3000\u3000')
            price_gnx=price_gnx[0].strip('\u3000\u3000')
            price_hzq=price_hzq[0].strip('\u3000\u3000')
            price_lyq=price_lyq[0].strip('\u3000\u3000')
            price_gyq=price_gyq[0].strip('\u3000\u3000')
            price_avg_1=price_avg_1[0].strip('\u3000\u3000')
            price_avg_2=price_avg_2[0].strip('\u3000\u3000')
            if price_dhx=="<br>":
                price_dhx=0
            if price_gyx=="<br>":
                price_gyx=0
            if price_gnx=="<br>":
                price_gnx=0
            if price_hzq=="<br>":
                price_hzq=0
            if price_lyq=="<br>":
                price_lyq=0
            if price_gyq=="<br>":
                price_gyq=0

            price_sum=float(price_avg_1)+float(price_avg_2)
            info = {"name": name_arr[0],
                    "type": type_arr[0],
                    'price_dhx': price_dhx,
                    'price_gyx': price_gyx,
                    'price_gnx': price_gnx,
                    'price_hzq': price_hzq,
                    'price_lyq': price_lyq,
                    'price_gyq': price_gyq,
                    'price_avg': round(price_sum/2,2),
                    'publishDate': time_arr[0]+" 00:00",
                    'title': title_arr[0],
                    }
            now_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            print(info)
            find_info = mysqldb.table_select_one(table="agri_price_lyg", field="id", where={"title": title_arr[0],"name":name_arr[0]})
            # 存在数据则更新
            if find_info:
                info['update_date'] = now_time
                mysqldb.table_update(table="agri_price_lyg", data=info,
                                     where={"title": title_arr[0],
                                            "name": name_arr[0]})
            else:
                info['create_date'] = now_time
                mysqldb.table_insert(table="agri_price_lyg", data=info)

    # 关闭
    await page.close()
    await browser.close()
    exit()


class Egas:
    def __init__(self):
        self.url = "http://zs.jsncpaq.com/"
        self.main()

    def main(self):
        # 登录
        asyncio.get_event_loop().run_until_complete(login())

Egas()
